package jax.lang;

/**
 * ...
 * @author tom
 */
class JSystem {
	public static var err:jax.io.JPrintStream = 
	#if sys
		new jax.io.JPrintStream(new jax.io.JOutputStream(Sys.stderr()));
	#else
		null;
	#end
	public static var in__:jax.io.JInputStream = 
	#if sys
		new jax.io.JInputStream(Sys.stdin());
	#else
		null;
	#end
	public static var out = 
	#if sys
		new jax.io.JPrintStream(new jax.io.JOutputStream(Sys.stdout()));
	#else
		new JOutputStream();
	#end
	//static var properties:jax.util.JProperties;
	public static function getIn() {
		return in__;
	}
	public static function nanoTime():haxe.Int64 {
		return haxe.Int64.mul(haxe.Int64.ofInt(Std.int(haxe.Timer.stamp())), haxe.Int64.ofInt(1000));
	}
}
class JOutputStream implements jax.io.JPrintStream {
	var buff:StringBuf;
	static var NEW_LINE = "\n";
	public function new() {
		buff = new StringBuf();
	}
	public function println(s:Dynamic) {
		print(s);
		print(NEW_LINE);
	}
	public function print(s:Dynamic) {
		var as = Std.string(s);
		buff.add(as);
		if(as.indexOf(NEW_LINE) == -1) {
			flush();
		}
	}
	public function flush() {
		var as = buff.toString();
		buff = new StringBuf();
		for(l in as.split("\n")) {
			if(l.length > 0) {
				#if sys
					Sys.println(l);
				#elseif js
					untyped console.log(l);
					var out = js.Lib.document.getElementById("java-output");
					if(out != null)
						out.innerHTML += l + "<br>";
				#else
					trace(l);
				#end
			}
		}
	}
}